package com.fancon.android.emoji;

import java.io.IOException;

import com.fancon.android.emoji.EmojiUtil.EmojiConfigurationException;


import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.widget.Button;

/**
 * Used to display image of an emoji on screen
 * 
 * @author Wazi Armstrong
 * 
 */
public class Emoji extends Button {//ImageButton

	static final String TAG = Emoji.class.getName();

	/**
	 * A Unicode character that describe this emoji. Maybe 1 or 2 char(s)
	 */
	String emojiCode;
	/**
	 * Category of this emojis
	 */
	int category;

	/**
	 * True if this emoji is an animation
	 */
	boolean isAnimation;

	int mWidth, mHeight;

	/**
	 * Used to hold the image or animation of this emoji
	 */
	private Drawable mDrawable;
	private String emojiName;
	/**
	 * Create an instance of this emoji
	 * 
	 * @param code
	 *            A hexa decimal string that describe this emoji
	 * @param category
	 * @param isAnimated
	 *            True if this emoji is an animation
	 * @throws EmojiConfigurationException
	 * @throws IOException
	 * @throws Exception
	 */
	public Emoji(Context context, String code, int category, boolean isAnimated, Drawable drawable, String name) {
		super(context);

		//setScaleType(ScaleType.CENTER);
		//setScaleType(ScaleType.FIT_XY);
		mDrawable = drawable;
		//setBackgroundDrawable(context.getResources().getDrawable(R.drawable.cell_selector));
		setBackgroundDrawable(mDrawable);
		//setImageDrawable(mDrawable);
		// Set width and height
		mWidth = mDrawable.getIntrinsicWidth();
		mHeight = mDrawable.getIntrinsicHeight();
		this.emojiName = name;
		if (isAnimated) {
			// Correct width and height
			mWidth = ((AnimationDrawable) mDrawable).getFrame(0).getIntrinsicWidth();
			mHeight = ((AnimationDrawable) mDrawable).getFrame(0).getIntrinsicHeight();
			// Start Animation
			this.post(new Runnable() {

				public void run() {
					((AnimationDrawable) mDrawable).start();
				}
			});
		}
		//mDrawable.setBounds(0, 0, mWidth, mHeight);
		emojiCode = code;
		this.category = category;
	}

	/*	@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			Paint p = new Paint();
			p.setColor(0xffff0000);
			canvas.drawRect(0, 0, 5, 5, p);
		}*/

	public String getCode() {
		return emojiCode;
	}

	public int getCategory() {
		return category;
	}
	public boolean isAnimated() {
		return isAnimation;
	}
	public int getIntrinsicWidth() {
		return mWidth;
	}

	public int getIntrinsicHeight() {
		return mHeight;
	}

	//@Override
	public Drawable getDrawable() {
		return mDrawable;

	}

	public String getEmojiName() {
		return emojiName;
	}

	public void setEmojiName(String emojiName) {
		this.emojiName = emojiName;
	}

	@Override
	public boolean equals(Object o) {
		// TODO Auto-generated method stub
		return emojiName.equals(((Emoji)o).getEmojiName());
	}
	
}
